<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-statvfs.html" />
<link rel="prev" href="module-fileinput.html" />
<link rel="parent" href="filesys.html" />
<link rel="next" href="module-statvfs.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>11.3 stat -- Interpreting stat() results</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="11.2 fileinput  "
  href="module-fileinput.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="11. file and Directory"
  href="filesys.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="11.4 statvfs  "
  href="module-statvfs.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-fileinput.html">11.2 fileinput  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="filesys.html">11. File and Directory</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-statvfs.html">11.4 statvfs  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0013300000000000000000">
11.3 <tt class="module">stat</tt> --
         Interpreting <tt class="function">stat()</tt> results</a>
</h1>

<p>
<a name="module-stat"></a>

<p>
The <tt class="module">stat</tt> module defines constants and functions for
interpreting the results of <tt class="function">os.stat()</tt>,
<tt class="function">os.fstat()</tt> and <tt class="function">os.lstat()</tt> (if they exist).  For
complete details about the <tt class="cfunction">stat()</tt>, <tt class="cfunction">fstat()</tt> and
<tt class="cfunction">lstat()</tt> calls, consult the documentation for your system.

<p>
The <tt class="module">stat</tt> module defines the following functions to test for
specific file types:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2217' xml:id='l2h-2217' class="function">S_ISDIR</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return non-zero if the mode is from a directory.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2218' xml:id='l2h-2218' class="function">S_ISCHR</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return non-zero if the mode is from a character special device file.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2219' xml:id='l2h-2219' class="function">S_ISBLK</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return non-zero if the mode is from a block special device file.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2220' xml:id='l2h-2220' class="function">S_ISREG</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return non-zero if the mode is from a regular file.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2221' xml:id='l2h-2221' class="function">S_ISFIFO</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return non-zero if the mode is from a FIFO (named pipe).
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2222' xml:id='l2h-2222' class="function">S_ISLNK</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return non-zero if the mode is from a symbolic link.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2223' xml:id='l2h-2223' class="function">S_ISSOCK</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return non-zero if the mode is from a socket.
</dl>

<p>
Two additional functions are defined for more general manipulation of
the file's mode:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2224' xml:id='l2h-2224' class="function">S_IMODE</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return the portion of the file's mode that can be set by
<tt class="function">os.chmod()</tt>--that is, the file's permission bits, plus the
sticky bit, set-group-id, and set-user-id bits (on systems that support
them).
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2225' xml:id='l2h-2225' class="function">S_IFMT</tt></b>(</nobr></td>
  <td><var>mode</var>)</td></tr></table></dt>
<dd>
Return the portion of the file's mode that describes the file type (used
by the <tt class="function">S_IS*()</tt> functions above).
</dl>

<p>
Normally, you would use the <tt class="function">os.path.is*()</tt> functions for
testing the type of a file; the functions here are useful when you are
doing multiple tests of the same file and wish to avoid the overhead of
the <tt class="cfunction">stat()</tt> system call for each test.  These are also
useful when checking for information about a file that isn't handled
by <tt class="module"><a href="module-os.path.html">os.path</a></tt>, like the tests for block and character
devices.

<p>
All the variables below are simply symbolic indexes into the 10-tuple
returned by <tt class="function">os.stat()</tt>, <tt class="function">os.fstat()</tt> or
<tt class="function">os.lstat()</tt>.

<p>
<dl><dt><b><tt id='l2h-2226' xml:id='l2h-2226'>ST_MODE</tt></b></dt>
<dd>
Inode protection mode.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2227' xml:id='l2h-2227'>ST_INO</tt></b></dt>
<dd>
Inode number.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2228' xml:id='l2h-2228'>ST_DEV</tt></b></dt>
<dd>
Device inode resides on.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2229' xml:id='l2h-2229'>ST_NLINK</tt></b></dt>
<dd>
Number of links to the inode.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2230' xml:id='l2h-2230'>ST_UID</tt></b></dt>
<dd>
User id of the owner.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2231' xml:id='l2h-2231'>ST_GID</tt></b></dt>
<dd>
Group id of the owner.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2232' xml:id='l2h-2232'>ST_SIZE</tt></b></dt>
<dd>
Size in bytes of a plain file; amount of data waiting on some special
files.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2233' xml:id='l2h-2233'>ST_ATIME</tt></b></dt>
<dd>
Time of last access.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2234' xml:id='l2h-2234'>ST_MTIME</tt></b></dt>
<dd>
Time of last modification.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2235' xml:id='l2h-2235'>ST_CTIME</tt></b></dt>
<dd>
The ``ctime'' as reported by the operating system.  On some systems
(like <span class="Unix">Unix</span>) is the time of the last metadata change, and, on others
(like Windows), is the creation time (see platform documentation for
details).
</dd></dl>

<p>
The interpretation of ``file size'' changes according to the file
type.  For plain files this is the size of the file in bytes.  For
FIFOs and sockets under most flavors of <span class="Unix">Unix</span> (including Linux in
particular), the ``size'' is the number of bytes waiting to be read at
the time of the call to <tt class="function">os.stat()</tt>, <tt class="function">os.fstat()</tt>,
or <tt class="function">os.lstat()</tt>; this can sometimes be useful, especially for
polling one of these special files after a non-blocking open.  The
meaning of the size field for other character and block devices varies
more, depending on the implementation of the underlying system call.

<p>
Example:

<p>
<div class="verbatim"><pre>
import os, sys
from stat import *

def walktree(top, callback):
    '''recursively descend the directory tree rooted at top,
       calling the callback function for each regular file'''

    for f in os.listdir(top):
        pathname = os.path.join(top, f)
        mode = os.stat(pathname)[ST_MODE]
        if S_ISDIR(mode):
            # It's a directory, recurse into it
            walktree(pathname, callback)
        elif S_ISREG(mode):
            # It's a file, call the callback function
            callback(pathname)
        else:
            # Unknown file type, print a message
            print 'Skipping %s' % pathname

def visitfile(file):
    print 'visiting', file

if __name__ == '__main__':
    walktree(sys.argv[1], visitfile)
</pre></div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="11.2 fileinput  "
  href="module-fileinput.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="11. file and Directory"
  href="filesys.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="11.4 statvfs  "
  href="module-statvfs.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-fileinput.html">11.2 fileinput  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="filesys.html">11. File and Directory</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-statvfs.html">11.4 statvfs  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
